package com.higgins.client.agentapp.components;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SelectElement;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.higgins.client.GreetingService;
import com.higgins.client.GreetingServiceAsync;
import com.higgins.client.agentapp.datamodel.AgencyDTO;
import com.higgins.client.agentapp.datamodel.JobSiteDTO;
import com.higgins.client.agentapp.service.AgentAppService;
import com.higgins.client.agentapp.service.AgentAppServiceAsync;
import com.higgins.client.dataModel.CoveringLetterDTO;
import com.higgins.client.dataModel.RecipientCountDTO;
import com.higgins.client.dataModel.StatusDTO;
import com.higgins.client.dataModel.StrategyDTO;
import com.higgins.client.dataModel.StrategyRecipientCountDTO;
import com.higgins.client.dataModel.StrategyRecipientDTO;
import com.higgins.client.generic.components.GenericColumn;
import com.higgins.client.generic.components.GenericTable;

public class AgencyTable extends GenericTable	{
	private static final Logger log = Logger.getLogger(AgencyTable.class.getName());
	private final AgentAppServiceAsync agentAppService = GWT
			.create(AgentAppService.class);
	
	public AgencyTable(AgencyColumns columns)	{
		super(columns);
		retrieveDTOs();
	}
	
	protected void addObject(int row, int column, Object object)	{
		ensureRowAvailable(row);
		GenericColumn genericColumn = genericColumns.getColumn(column);
//		if (genericColumn.getType().equals(StatusDTO.class))	{
//			addStatusDTO(row, column, object, genericColumn);
//		}
	}
	
	private List<List<Object>> convertDTOtoRowArray(List<AgencyDTO> agencyDTOs)	{
		List<List<Object>> rows = new ArrayList<List<Object>>();
		for (AgencyDTO agencyDTO:agencyDTOs)	{
			List<Object> row = new ArrayList<Object>();
			row.add(agencyDTO.getId());
			row.add(agencyDTO.getName());
			row.add(agencyDTO.getAddress());
			row.add(agencyDTO.getPhone());
			row.add(agencyDTO.getWebsite());
			rows.add(row);
		}
		return rows;
	}
	
	public void updateItemCell(int row)	{
		AgencyDTO agencyDTO = new AgencyDTO();
		agencyDTO.setId(getId(row));
		agencyDTO.setName(getText(row, 1));
		agencyDTO.setAddress(getText(row, 2));
		agencyDTO.setPhone(getText(row, 3));
		agencyDTO.setWebsite(getText(row, 4));
		updateRow(agencyDTO);
	}
	
	protected void retrieveDTOs()	{
		try {
			agentAppService.retrieveAgencys(new AsyncCallback<List<AgencyDTO>>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("FixedOutgoingDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(List<AgencyDTO> result) {
						loadData(convertDTOtoRowArray(result));
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("FixedOutgoingDTO: " + e.toString());
		} 
	}	
	
	private void updateRow(AgencyDTO agencyDTO)	{
		try {
			agentAppService.updateAgency(agencyDTO, new AsyncCallback<String>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("FixedOutgoingDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(String result) {
						setRowIdAfterUpdate(result);
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("FixedOutgoingDTO: " + e.toString());
		}
	}
}